package cn.signUpSystem.listener;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.util.DigestUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.signUpSystem.pojo.Admin;
import cn.signUpSystem.pojo.MyTime;
import cn.signUpSystem.pojo.Position;
import cn.signUpSystem.service.AdminService;
import cn.signUpSystem.service.MyTimeService;
import cn.signUpSystem.service.PositionService;
import cn.signUpSystem.util.TimeUtil;

/**
 * 完成项目启动数据的初始化功能
 * 
 * @author ASUS
 * 
 */
public class SignUpSystemServletContextListener implements
		ServletContextListener {

	private AdminService adminService = null;

	private PositionService positionService = null;

	private MyTimeService myTimeService = null;

	private ApplicationContext context = null;

	private List<String> grades = null;

	private List<Position> positions = null;

	public void contextDestroyed(ServletContextEvent se) {
		// TODO Auto-generated method stub
	}

	public void contextInitialized(ServletContextEvent se) {
		// TODO Auto-generated method stub
		context = WebApplicationContextUtils.getWebApplicationContext(se
				.getServletContext());
		adminService = (AdminService) context.getBean("adminService");
		positionService = (PositionService) context.getBean("positionService");
		myTimeService = (MyTimeService) context.getBean("myTimeService");

		// 创建一个管理员
		if (adminService.queryAll() == null) {
			Admin admin = new Admin();
			admin.setName("admin");
			admin.setPassword(DigestUtils.md5DigestAsHex(("123").getBytes()));
			adminService.add(admin);
		}

		// 插入职位记录
		if (positionService.queryAll() == null) {
			Set<Position> positions = new HashSet<Position>();

			Position position = new Position();
			position.setName("开发");
			position.setDetail("web前端：JavaScript，css，html;web后台：php，go，python;其他：安卓，ios");

			Position position4 = new Position();
			position4.setName("web前端");
			position4.setDetail("JavaScript，css，html");
			positions.add(position4);

			Position position5 = new Position();
			position5.setName("web后台");
			position5.setDetail("php，go，python，java");
			positions.add(position5);

			Position position6 = new Position();
			position6.setName("其他");
			position6.setDetail("安卓，ios");
			positions.add(position6);

			position4.setParentPosition(position);
			position5.setParentPosition(position);
			position6.setParentPosition(position);

			Position position1 = new Position();
			position1.setName("设计");
			position1
					.setDetail("设计师：红满堂旗下网站的UI设计,设计论坛活动的宣传资料，进行线上线下的美工工作，掌握Photoshop等工具，有设计经验者优先");

			Position position2 = new Position();
			position2.setName("版主");
			position2
					.setDetail("版主：红满堂社区中占比例最大，也是最为重要的一个群体。既负责运营版块，也负责管理版块。你的版块你作主！");

			Position position3 = new Position();
			position3.setName("运营");
			position3
					.setDetail("总有适合你的!负责红满堂微博微信的日常更新。Neeeded：1.热爱生活，善于捕捉校园生活中的热点事物。2.文字表达能力较强。");

			positionService.add(position);
			positionService.add(position1);
			positionService.add(position2);
			positionService.add(position3);
			positionService.add(position4);
			positionService.add(position5);
			positionService.add(position6);
		}
		positions = positionService.queryAll();
		se.getServletContext().setAttribute("positions", positions);

		// 一开始没设置过期时间的话就默认为当前时间加一个月
		if (myTimeService.queryAll() == null) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);// 获取年份
			int month = cal.get(Calendar.MONTH);// 获取月份
			month += 2;
			int day = cal.get(Calendar.DATE);// 获取日
			if (month >= 12) {
				year += 1;
				month = month - 11;
			}
			MyTime preTime = new MyTime();
			preTime.setYear(String.valueOf(year));
			preTime.setMonth(String.valueOf(month));
			preTime.setDay(String.valueOf(day));
			myTimeService.add(preTime);
		}

		// 保存年级在application域中
		grades = new ArrayList<String>();
		grades.add("2013");
		grades.add("2014");
		grades.add("2015");
		se.getServletContext().setAttribute("grades", grades);

		// 查询报名时间是否已经过期
		MyTime myTime = myTimeService.queryAll().get(0);
		boolean timeIsEffective = TimeUtil.judgeTime(myTime.getYear(),
				myTime.getMonth(), myTime.getDay());
		se.getServletContext().setAttribute("timeIsEffective", timeIsEffective);
	}
}
